package com.hy.mydesktop.client.component.factory;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.TreePanelModelEnum;
import com.hy.mydesktop.client.component.meta.TreePanelModulesModelEnum;
import com.hy.mydesktop.client.rpc.ServiceInvoker;
import com.hy.mydesktop.client.rpc.ServiceInvokerAsync;
import com.hy.mydesktop.client.rpc.reader.GxtToSeamServiceResultListReader;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;

/**
 * 
 * <ul>
 * <li>开发作者：冬超</li>
 * <li>设计日期：2010-8-30；时间：下午01:11:56</li>
 * <li>类型名称：ReorderingGridFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class TreePanelModulesFactory2 {

	private static ServiceInvokerAsync service = GWT
			.create(ServiceInvoker.class);
	// 数据加载
	private static TreeStore<ModelData> store;
	// 面板
	private static ContentPanel maxPanel;

	private static TreeLoader<ModelData> loader;

	private static GxtToSeamServiceParameter gxtToSeamServiceParameter;

	private static GxtToSeamServiceResultListReader<ModelData> reader = new GxtToSeamServiceResultListReader<ModelData>();

	public static ContentPanel createTreePanelModule(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		return configureTreePanel(gxtComponentMetaNodeModel);
	}

	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static ContentPanel configureTreePanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		String table = (String) gxtComponentMetaNodeModel
				.get(TreePanelModulesModelEnum.TABLE.name().toLowerCase());

		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.LOADERDATA.name()
				.toLowerCase()) != null) {
			gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter
					.setServiceComponentName("com.test.server.business.PersonService");
			gxtToSeamServiceParameter.setServiceMethodName("listPerson2");
			BaseModel[] methodArguments = new BaseModel[1];
			methodArguments[0] = new BaseModel();
			gxtToSeamServiceParameter.setMethodArguments(methodArguments);
		}

		final TreePanel<ModelData> treepanel = createDefaultBaseTreePanel(
				gxtComponentMetaNodeModel, table);

		// 高度
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.HIGHT.name()
				.toLowerCase()) != null) {
			Integer height = (Integer) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.HIGHT.name().toLowerCase());
			treepanel.setHeight((int) height);

		}
		// 宽度
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.WIDTH.name()
				.toLowerCase()) != null) {
			Integer width = (Integer) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.WIDTH.name().toLowerCase());
			treepanel.setWidth((int) width);

		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.AUTOLOAD.name()
				.toLowerCase()) != null) {
			Boolean autoLoad = (Boolean) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.AUTOLOAD.name().toLowerCase());
			treepanel.setAutoLoad(autoLoad);

		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.ADDBUTTON.name()
				.toLowerCase()) != null) {
			Button expend = ButtonFactory
					.createButton(gxtComponentMetaNodeModel);
			expend.addListener(Events.OnClick, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					treepanel.expandAll();

				}
			});
			Button collapse = ButtonFactory
					.createButton(gxtComponentMetaNodeModel);
			collapse.addListener(Events.OnClick, new Listener<ButtonEvent>() {

				@Override
				public void handleEvent(ButtonEvent be) {
					treepanel.collapseAll();

				}
			});

			maxPanel.add(expend);
			maxPanel.add(collapse);

		}
		// 需要把方filter加到面板上才能够使用
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.FILTER.name()
				.toLowerCase()) != null) {
			StoreFilterField<ModelData> filter = new StoreFilterField<ModelData>() {

				@Override
				protected boolean doSelect(Store<ModelData> store,
						ModelData parent, ModelData record, String property,
						String filter) {

					return false;
				}

			};
			filter.bind(store);
			maxPanel.setTopComponent(filter);
		}

		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.TRACKMOUSEOVER
				.name().toLowerCase()) != null) {
			Boolean trackMouseOver = (Boolean) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.TRACKMOUSEOVER.name().toLowerCase());
			treepanel.setTrackMouseOver(trackMouseOver);

		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.BORDER.name()
				.toLowerCase()) != null) {
			Boolean show = (Boolean) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.BORDER.name().toLowerCase());
			treepanel.setBorders(show);

		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.CHECKBOXSTYLE
				.name().toLowerCase()) != null) {
			String checkable = (String) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.CHECKBOXSTYLE.name().toLowerCase());
			if (checkable.equals("parents")) {
				treepanel.setCheckStyle(CheckCascade.PARENTS);
			} else if (checkable.equals("children")) {
				treepanel.setCheckStyle(CheckCascade.CHILDREN);

			} else {
				treepanel.setCheckStyle(CheckCascade.NONE);
			}
		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.CHECKBOXNODE
				.name().toLowerCase()) != null) {
			String checkable = (String) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.CHECKBOXNODE.name().toLowerCase());
			if (checkable.equals("parent")) {
				treepanel.setCheckNodes(CheckNodes.PARENT);
			} else if (checkable.equals("leaf")) {
				treepanel.setCheckNodes(CheckNodes.LEAF);

			} else if (checkable.equals("both")) {
				treepanel.setCheckNodes(CheckNodes.BOTH);
			}
		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.CHECKBOX.name()
				.toLowerCase()) != null) {
			Boolean checkable = (Boolean) gxtComponentMetaNodeModel
					.get(TreePanelModulesModelEnum.CHECKBOX.name().toLowerCase());
			treepanel.setCheckable(checkable);

		}
		if (gxtComponentMetaNodeModel.get(TreePanelModulesModelEnum.MENU.name()
				.toLowerCase()) != null) {
			Menu contextMenu = MenuFactory
					.createMenu(gxtComponentMetaNodeModel);

			/*
			 * MenuItem insert = new MenuItem(); insert.setText("Insert Item");
			 * insert.addSelectionListener(new SelectionListener<MenuEvent>() {
			 * public void componentSelected(MenuEvent ce) { ModelData folder =
			 * treepanel.getSelectionModel().getSelectedItem(); if (folder !=
			 * null) { RPCTreeModel child = new RPCTreeModel("Add Child ","");
			 * store.add(folder, child, false); treepanel.setExpanded(folder,
			 * true); } } }); contextMenu.add(insert); MenuItem remove = new
			 * MenuItem(); remove.setText("Remove Selected");
			 * remove.addSelectionListener(new SelectionListener<MenuEvent>() {
			 * public void componentSelected(MenuEvent ce) { List<ModelData>
			 * selected = treepanel.getSelectionModel().getSelectedItems(); for
			 * (ModelData sel : selected) { store.remove(sel); } } });
			 * contextMenu.add(remove);
			 */
			treepanel.setContextMenu(contextMenu);

		}

		return maxPanel;
	}

	private static TreePanel<ModelData> createDefaultBaseTreePanel(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel, String table) {

		maxPanel = ContentPanelFactory
				.createContentPanel(gxtComponentMetaNodeModel);

		store = getTreePanelStore(table);

		TreePanel<ModelData> treePanel = new TreePanel<ModelData>(store);
		treePanel.setDisplayProperty("name");
		maxPanel.add(treePanel);
		return treePanel;
	}

	private static TreeStore<ModelData> getTreePanelStore(final String table) {
		RpcProxy<GxtToSeamServiceResult> proxy = new RpcProxy<GxtToSeamServiceResult>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<GxtToSeamServiceResult> callback) {
				service.callService(gxtToSeamServiceParameter, callback);

			}
		};
		// 判断是否有孩子
		loader = new BaseTreeLoader<ModelData>(proxy, reader){

			@Override
			public boolean hasChildren(ModelData parent) {
				return super.hasChildren(parent);
			}
			
			
		};
		
		store = new TreeStore<ModelData>(loader);
		// 排序
		//store.setStoreSorter(new StoreSorter<ModelData>());
		return store;
	}
}
